<template>
  <div>
    <el-select
        :loading="loading"
        clearable
        filterable
        placeholder="请选择巡查分类"
        class="ele-block"
        :value="value"
        @change="change">
      <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id" />
    </el-select>
    <el-input v-model="pname" placeholder="自动带出上级"></el-input>
  </div>
</template>

<script>
export default {
  props: {
    value: String,
    model_type:String
  },
  model: {
    prop: 'value',
    event: 'change',
  },
  data: function () {
    return {
      deptList: [],
      pname:'',
      loading: false,

    };
  },
  mounted() {
    this.getParentList()
  },
  methods: {
    async getParentList(){
      this.loading = true
      this.$http.post('/abnormal/getparentList?level=2&model_type='+this.model_type).then(res => {
        for (let i in res.data.data) {
            this.deptList.push({
              ...res.data.data[i],
              id: res.data.data[i].id.toString()
            })
          }
        this.loading = false
        this.getpname(this.value)
      }).catch(e => {
        this.loading = false
        this.$message.error(e.message);
      });
    },
    getpname(id){
      console.log(id)
      this.deptList.forEach(items=>{
        if(items.id==id){
          this.pname=items.pname
          console.log(this.pname)
        }
      })
    },
    change(val) {
      this.getpname(val)
      this.$emit('change', val);
    }
  },
};
</script>